package com.zzyl.nursing.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;
import com.zzyl.nursing.service.impl.ContractServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component("updateConstractStatusJob")
public class UpdateConstractStatusJob {
    @Autowired
    private IContractService  contractService;

    public void noParams() {
//        System.out.println("执行无参方法");
//        //获取合同
//        List<Contract> list = contractService.list();
//        for (Contract contract : list) {
//            if(contract.getStartDate().isAfter(LocalDateTime.now())){
//                contract.setStatus(0);
//            }else if (contract.getStartDate().isBefore(LocalDateTime.now())&&contract.getEndDate().isAfter(LocalDateTime.now())){
//                contract.setStatus(1);
//            }else if(contract.getEndDate().isBefore(LocalDateTime.now())){
//                contract.setStatus(2);
//            }
//            //更新
//            contractService.updateContract(contract);
//        }

        //未生效-已生效
        LambdaUpdateWrapper<Contract> updateWrapper = new LambdaUpdateWrapper<>();
        //update 表 set status = 1 where status = 0 and startDate < now() and endDate > now()
        updateWrapper.eq(Contract::getStatus,0);
        updateWrapper.set(Contract::getStatus,1);
        updateWrapper.lt(Contract::getStartDate,LocalDateTime.now());
        updateWrapper.gt(Contract::getEndDate,LocalDateTime.now());
        contractService.update(updateWrapper);
        //已生效-已失效
        LambdaUpdateWrapper<Contract> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(Contract::getStatus,1);
        updateWrapper1.set(Contract::getStatus,2);
        updateWrapper1.lt(Contract::getEndDate,LocalDateTime.now());
        contractService.update(updateWrapper1);





    }

}
